import * as vscode from "vscode";
import { helloWorldCommand } from "../plugins/actions/HelloWorld/HelloWorld";
import { addComment } from "../plugins/actions/AddComment/addComment";
import { VerticalDiffManager } from "./diff/vertical/manager";
import { LLMFactory } from "../core/llm/llmFactory";
import { HarmonyPilotGUIWebviewViewProvider } from "./HarmonyPilotGUIWebviewViewProvider";
import { focusInput } from "../plugins/actions/FocusInput/focusInput";

// Copy everything over from extension.ts
const getCommandsMap: (verticalDiffManager: VerticalDiffManager, llmFactory: LLMFactory, sidebar: HarmonyPilotGUIWebviewViewProvider) 
=> { [command: string]: (...args: any) => any } = 
(verticalDiffManager: VerticalDiffManager, llmFactory: LLMFactory, sidebar: HarmonyPilotGUIWebviewViewProvider) => {
    return {
        "HarmonyPilot.focusInput": async () => {
            focusInput(sidebar);
        },

        "HarmonyPilot.newChatBtnClick": async () => {
            //TODO: 实现New Chat命令
        },

        "HarmonyPilot.chatHistoryBtnClick": async () => {
            //TODO: 实现Chat History命令
        },

        "HarmonyPilot.helpBtnClick": async () => {
            //TODO: 实现Help命令
        }, 

        "HarmonyPilot.issueReportBtnClick": async () => {
            //TODO: 实现Issue Report命令
        },

        "HarmonyPilot.settingBtnClick": async () => {   
            //TODO: 实现Setting命令
        },

        // key是命令id，value是命令的回调函数
        "HarmonyPilot.helloWorld": async () => {
            helloWorldCommand();
        },

        "HarmonyPilot.rejectVerticalDiffBlock": async(fileUri?: string, index?: number) => {
            verticalDiffManager.acceptRejectVerticalDiffBlock(false, fileUri, index);
        }, 

        "HarmonyPilot.acceptVerticalDiffBlock": async(fileUri?: string, index?: number) => {
            verticalDiffManager.acceptRejectVerticalDiffBlock(true, fileUri, index);
        },

        // DocLinter
        "HarmonyPilot.docLinter": async () => {
            //TODO: 实现DocLinter命令
        },
        
        // Explain Code
        "HarmonyPilot.explainCode": async () => {
            //TODO: 实现Explain Code命令
        },
        
        // Write Comment for Code
        "HarmonyPilot.writeCommentForCode": async () => {   
            addComment(verticalDiffManager, llmFactory);
        },
        
        // Optimize Code
        "HarmonyPilot.optimizeCode": async () => {
            //TODO: 实现Optimize Code命令
        }
    }
}

export function registerAllCommands(
    context: vscode.ExtensionContext,
    verticalDiffManager: VerticalDiffManager,
    llmFactory: LLMFactory,
    sidebar: HarmonyPilotGUIWebviewViewProvider
) {
    for (const [command, callback] of Object.entries(
        getCommandsMap(verticalDiffManager, llmFactory, sidebar)
    )) {
        // 注册所有命令
        context.subscriptions.push(
            vscode.commands.registerCommand(command, callback),
          );
    }
}
